package fag.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.EventListenerList;

public class ModelBar extends JMenuBar implements ActionListener {

	private static final long serialVersionUID = 8202066650826357057L;
	
	private JMenu mModel =  new JMenu("Model");
	private JMenuItem miNew = new JMenuItem("New");
	private JMenuItem miLoad = new JMenuItem("Load");
	private JMenuItem miSave = new JMenuItem("Save");
	
	protected final static String ACTION_NEW_MODEL = "New Model";
	protected final static String ACTION_LOAD_MODEL ="Load Model";
	protected final static String ACTION_SAVE_MODEL ="Save Model";
	
	private final EventListenerList listeners = new EventListenerList();
	

	public ModelBar() {
		
		super();
		
		miNew.addActionListener(this);
		miLoad.addActionListener(this);
		miSave.addActionListener(this);
		
		mModel.add(miNew);
		mModel.add(miLoad);
		mModel.add(miSave);
		
		add(mModel);
	}
	
	public void addActionListener(ActionListener listener) {
		
		listeners.add(ActionListener.class,listener);
	}
	
	public void removeActionListener(ActionListener listener) {
		
		listeners.remove(ActionListener.class, listener);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == miNew)
			newModel();
		
		if(e.getSource() == miSave)
			saveModel();
		
		if(e.getSource() == miLoad)
			loadModel();
	}
	
	protected void newModel() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_NEW_MODEL));
	}
	
	protected void saveModel() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_SAVE_MODEL));
	}

	protected void loadModel() {
	
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_LOAD_MODEL));
	}	

}
